//-----------------------------------------------------------------------
// <copyright file="UserDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class UserDAOBase
    {
        #region Common methods
        public virtual User CreateUserFromReader(IDataReader reader)
        {
            User item = new User();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("UserId"))) item.UserId = (long)reader["UserId"];
                if (!reader.IsDBNull(reader.GetOrdinal("UserName"))) item.UserName = (string)reader["UserName"];
                if (!reader.IsDBNull(reader.GetOrdinal("FullName"))) item.FullName = (string)reader["FullName"];
                if (!reader.IsDBNull(reader.GetOrdinal("Password"))) item.Password = (string)reader["Password"];
                if (!reader.IsDBNull(reader.GetOrdinal("Email"))) item.Email = (string)reader["Email"];
                if (!reader.IsDBNull(reader.GetOrdinal("Address"))) item.Address = (string)reader["Address"];
                if (!reader.IsDBNull(reader.GetOrdinal("Tel"))) item.Tel = (string)reader["Tel"];
                if (!reader.IsDBNull(reader.GetOrdinal("Cell"))) item.Cell = (string)reader["Cell"];
                if (!reader.IsDBNull(reader.GetOrdinal("Enable"))) item.Enable = (bool)reader["Enable"];
                if (!reader.IsDBNull(reader.GetOrdinal("Description"))) item.Description = (string)reader["Description"];
                if (!reader.IsDBNull(reader.GetOrdinal("PermissionId"))) item.PermissionId = (long)reader["PermissionId"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateUserFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateUser methods
            
        public virtual void CreateUser(User user)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spUserInsert");
                
                database.AddInParameter(dbCommand, "@UserName", DbType.String, user.UserName);
                database.AddInParameter(dbCommand, "@FullName", DbType.String, user.FullName);
                database.AddInParameter(dbCommand, "@Password", DbType.String, user.Password);
                database.AddInParameter(dbCommand, "@Email", DbType.String, user.Email);
                database.AddInParameter(dbCommand, "@Address", DbType.String, user.Address);
                database.AddInParameter(dbCommand, "@Tel", DbType.String, user.Tel);
                database.AddInParameter(dbCommand, "@Cell", DbType.String, user.Cell);
                database.AddInParameter(dbCommand, "@Enable", DbType.Boolean, user.Enable);
                database.AddInParameter(dbCommand, "@Description", DbType.String, user.Description);
                database.AddInParameter(dbCommand, "@PermissionId", DbType.Int64, user.PermissionId);
                database.AddOutParameter(dbCommand, "@UserId", DbType.Int64, 8);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                user.UserId = (long)database.GetParameterValue(dbCommand, "@UserId");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateUserException, ex);
            }
        }

        #endregion

        #region UpdateUser methods
        
        public virtual void UpdateUser(User user)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spUserUpdate");            
                
                database.AddInParameter(dbCommand, "@UserId", DbType.Int64, user.UserId);
                database.AddInParameter(dbCommand, "@UserName", DbType.String, user.UserName);
                database.AddInParameter(dbCommand, "@FullName", DbType.String, user.FullName);
                database.AddInParameter(dbCommand, "@Password", DbType.String, user.Password);
                database.AddInParameter(dbCommand, "@Email", DbType.String, user.Email);
                database.AddInParameter(dbCommand, "@Address", DbType.String, user.Address);
                database.AddInParameter(dbCommand, "@Tel", DbType.String, user.Tel);
                database.AddInParameter(dbCommand, "@Cell", DbType.String, user.Cell);
                database.AddInParameter(dbCommand, "@Enable", DbType.Boolean, user.Enable);
                database.AddInParameter(dbCommand, "@Description", DbType.String, user.Description);
                database.AddInParameter(dbCommand, "@PermissionId", DbType.Int64, user.PermissionId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateUserException, ex);
            }
        }
        
        #endregion

        #region DeleteUser methods
        public virtual void DeleteUser(long userId)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spUserDelete");
                
                database.AddInParameter(dbCommand, "@UserId", DbType.Int64, userId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteUserException, ex);
            }
        }

        #endregion

        #region GetUser methods
        
        public virtual User GetUser(long userId)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spUserGet");
                
                database.AddInParameter(dbCommand, "@UserId", DbType.Int64, userId);
                
                User user =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        user = CreateUserFromReader(reader);
                        reader.Close();
                    }
                }
                return user;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetUserException, ex);
            }
        }
        
        #endregion

        #region GetUserList methods
        public virtual UserCollection GetUserList(UserColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spUserGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                UserCollection userCollection = new UserCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        User user = CreateUserFromReader(reader);
                        userCollection.Add(user);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return userCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetUserListException, ex);
            }
        }
        
        public virtual UserCollection GetUserList(UserColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetUserList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetUserList methods
        public virtual UserCollection GetUserListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spUserGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                UserCollection userCollection = new UserCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        User user = CreateUserFromReader(reader);
                        userCollection.Add(user);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return userCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetUserListException, ex);
            }
        }
        
        public virtual UserCollection GetUserListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetUserListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}